package com.example.meterialproject.exts

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.meterialproject.adapter.TransitionAdapter
import com.example.meterialproject.bean.TransitionBean


/**
 *
 * @ClassName: RecyclerViewExt
 * @Author: android 超级兵
 * @CreateDate: 4/14/22$ 11:02 AM$
 * TODO
 */
fun RecyclerView.getCurrentScrollY(): Int {
    val layoutManager = this.layoutManager as LinearLayoutManager
    val position = layoutManager.findFirstVisibleItemPosition()
    val view = layoutManager.findViewByPosition(position)!!
    val itemHeight: Int = view.height
    return position * itemHeight - view.top
}

fun RecyclerView.createRecyclerView(
    context: Context,
    spanCount: Int = 2,
    callBack: (View, String, Int) -> Unit, // 点击事件回调
) = let {
    fun getData() = let {
        context.resources.assets.list("水果")?.filterNotNull()!!.map {
            it
        }.map {
            TransitionBean("file:///android_asset/水果/${it}", it.replace(".png", ""))
        }.toList()
    }

    layoutManager = GridLayoutManager(context, spanCount)
    return@let TransitionAdapter(context, getData()).also {
        adapter = it

        it.block = TransitionAdapter.CallBackListener { view, url, position ->
            callBack(view, url, position)
        }
    }
}